//
//  MyTeamViewController.m
//  Consume100
//
//  Created by eliot on 16/3/25.
//  Copyright © 2016年 aleven. All rights reserved.
//

#import "MyTeamViewController.h"
#import "TeamTableViewController.h"

#import "SecurityUtil.h"
#import "GTMBase64.h"

@interface MyTeamViewController ()

@property (weak, nonatomic) IBOutlet UIView *firstBGV;

@property (weak, nonatomic) IBOutlet UIView *secondBGV;

@property (weak, nonatomic) IBOutlet UIView *thridBGV;

@property (weak, nonatomic) IBOutlet UIView *fourthBGV;


@property (weak, nonatomic) IBOutlet UIView *firstView;


@property (weak, nonatomic) IBOutlet UIView *secondView;


@property (weak, nonatomic) IBOutlet UIView *thridView;


@property (weak, nonatomic) IBOutlet UIView *fourthView;


@property (weak, nonatomic) IBOutlet UIButton *addBtn;


@property (weak, nonatomic) IBOutlet UILabel *firstTeamLb;

@property (weak, nonatomic) IBOutlet UILabel *secondTeamLb;

@property (weak, nonatomic) IBOutlet UILabel *thirdTeamLb;


@property (weak, nonatomic) IBOutlet UITextField *inviterNumTextField;

/** 数据源*/
@property (nonatomic, strong) NSArray *dataArr;

@end

@implementation MyTeamViewController

+(MyTeamViewController*)CreatMyTeamViewController{
    
    UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    MyTeamViewController *myTeam=[story instantiateViewControllerWithIdentifier:@"MyTeamViewController"];
    return myTeam;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的团队";
    self.view.backgroundColor = [UIColor colorWithRed:232/255.0 green:233/255.0 blue:232/255.0 alpha:1];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationController.navigationBar.translucent = NO;
    
    self.inviterNumTextField.keyboardType = UIKeyboardTypeNumberPad;
    
    /** 设置背景色*/
    self.firstView.backgroundColor = [UIColor colorWithRed:255/255.0 green:88/255.0 blue:88/255.0 alpha:1];
    self.addBtn.backgroundColor = [UIColor colorWithRed:255/255.0 green:88/255.0 blue:88/255.0 alpha:1];;
    self.secondView.backgroundColor = [UIColor colorWithRed:212/255.0 green:180/255.0 blue:47/255.0 alpha:1];
    self.thridView.backgroundColor = [UIColor colorWithRed:93/255.0 green:198/255.0 blue:252/255.0 alpha:1];
    self.fourthView.backgroundColor = [UIColor colorWithRed:70/255.0 green:214/255.0 blue:141/255.0 alpha:1];
    /** 设置圆角*/
    self.addBtn.layer.cornerRadius = 5;
    self.addBtn.layer.masksToBounds = YES;
    self.firstBGV.layer.cornerRadius = 10;
    self.firstBGV.layer.masksToBounds = YES;
    self.secondBGV.layer.cornerRadius = 10;
    self.secondBGV.layer.masksToBounds = YES;
    self.thridBGV.layer.cornerRadius = 10;
    self.thridBGV.layer.masksToBounds = YES;
    self.fourthBGV.layer.cornerRadius = 10;
    self.fourthBGV.layer.masksToBounds = YES;
   
    UserModel *model  = userModel;
    
    /** 兑换商城数据*/
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 5;
    NSDictionary *dic = @{@"consumerId":[SecurityUtil encryptAESData:model.id]};
    NSString *baseUrl = [NSString stringWithFormat:@"%@myTeam.jhtml", MyBaseURL];
    [manager POST:baseUrl parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *err = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        self.dataArr = [err componentsSeparatedByString:@","];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            self.firstTeamLb.text = [NSString stringWithFormat:@"%@人", self.dataArr.firstObject];
            self.secondTeamLb.text = [NSString stringWithFormat:@"%@人", self.dataArr[1]];
            self.thirdTeamLb.text = [NSString stringWithFormat:@"%@人", self.dataArr[2]];
            
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:@"链接超时,请检查网络"];
        });
    }];
    
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.inviterNumTextField resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/** 添加按钮*/
- (IBAction)clickAddBtn:(id)sender {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"邀请码不存在或未注册" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];

    [alert addAction:action1];
    [self presentViewController:alert animated:YES completion:nil];
    
    
    
    
}


/** 点击一级团队*/
- (IBAction)clickFirstTeam:(id)sender {
    TeamTableViewController *vc = [TeamTableViewController CreatMakeExchageHistoryTableViewController];
    vc.teamTitle = @"一级团队";
    [self creatNav:vc];
    
}

/** 点击二级团队*/
- (IBAction)clickSecondTeam:(id)sender {
    TeamTableViewController *vc = [TeamTableViewController CreatMakeExchageHistoryTableViewController];
    vc.teamTitle = @"二级团队";
    [self creatNav:vc];
    
}

/** 点击三级团队*/
- (IBAction)clickThirdTeam:(id)sender {
    TeamTableViewController *vc = [TeamTableViewController CreatMakeExchageHistoryTableViewController];
    vc.teamTitle = @"三级团队";
    [self creatNav:vc];
}

//创建模态导航控制器
-(void)creatNav:(UIViewController *)view{
    
    UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:view];
    nav.navigationBar.barTintColor = [UIColor colorWithRed:254/255.0 green:154/255.0 blue:37/255.0 alpha:1];
    view.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_back"] style:UIBarButtonItemStylePlain target:self action:@selector(dismiss)];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)dismiss {
    [self dismissViewControllerAnimated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
